﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Net;
public partial class BackGround_recommend : System.Web.UI.Page
{
    Operation operation = new Operation();
    string infoid;
    string infotype="";
    string searchtype = "企业用户";
    
    protected void Page_Load(object sender, EventArgs e)
    {
       //Response.Write(Request.QueryString["id"].ToString() + "   " + Request.QueryString["type"].ToString());
        infoid = Request.QueryString["id"].ToString();
        
        if (!IsPostBack)
        {
            
              //infoid = Request.QueryString["id"].ToString();
            try
            {
                GridViewBind(searchtype);
                /*DataSet ds = operation.SelectInfo(Convert.ToInt32(infoid));
                txtType.Text = ds.Tables[0].Rows[0][1].ToString();
                txtTitle.Text = ds.Tables[0].Rows[0][2].ToString();
                txtInfo.Text = ds.Tables[0].Rows[0][3].ToString();
                txtLinkMan.Text = ds.Tables[0].Rows[0][4].ToString();
                txtTel.Text = ds.Tables[0].Rows[0][5].ToString();
                date.Text = ds.Tables[0].Rows[0][7].ToString(); */
            }
            catch (Exception ex) {  }
              //GridViewBind(searchtype);
              //if(infotype.IndexOf("需求")!=-1)searchtype=
          }
      }
      private void GridViewBind(string type)
      {
         
          GridView1.DataSource = operation.SelectUserInfoByType(type);
          GridView1.DataKeyNames = new string[] { "id" };
          
          GridView1.DataBind();
          //显示当前页数
          lblPageSum.Text = "当前页为　" + (GridView1.PageIndex + 1) + " / " + GridView1.PageCount + "　页";
         
      }
      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              //高亮显示指定行
              e.Row.Attributes.Add("onMouseOver", "Color=this.style.backgroundColor;this.style.backgroundColor='#FFF000'");
              e.Row.Attributes.Add("onMouseOut", "this.style.backgroundColor=Color;");
              //设置审核状态，并且设置相应的颜色。
              if (e.Row.Cells[4].Text == "False")
              {
                  e.Row.Cells[4].Text = StringFormat.HighLight("未激活", true);
              }
              else
              {
                  e.Row.Cells[4].Text = StringFormat.HighLight("已激活", false);
              }
             
            //多余字　使用...显示
           // e.Row.Cells[2].Text = StringFormat.Out(e.Row.Cells[2].Text, 18);

        }
    }
    
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
        Response.Write("<script> window.open('UserBaseInfo.aspx?id=" + id + "','','height=300,width=679,top=200,left=200') </script>");
        Response.Write("<script>history.go(-1)</script>");
        //userMsg.Text="<iframe id=\"iframe1\" name=\"mainFrame\" style=\"width: 585px; height: 596px\" frameborder=\"0\" src=\""+
        //    "UserBaseInfo.aspx?id=" + id + " \"></iframe>";
       // userMsg.Visible = true;
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //分页设置
        GridView1.PageIndex = e.NewPageIndex;
        
        GridViewBind(searchtype);
        //显示当前页数
        lblPageSum.Text = "当前页为　" + (GridView1.PageIndex + 1) + " / " + GridView1.PageCount + "　页";
    }
    protected void rdoBtnCheckBus_CheckedChanged(object sender, EventArgs e)
    {
        searchtype = "企业用户";
        GridViewBind(searchtype);
        //显示当前页数
        lblPageSum.Text = "当前页为　" + (GridView1.PageIndex + 1) + " / " + GridView1.PageCount + "　页";
       /* GridView1.PageIndex = 0;
        GridView1.DataSource = operation.SelectInfo(infoType, true);
        GridView1.DataBind();
        CheckType = 1;
        //显示当前页数
        lblPageSum.Text = "当前页为　" + (GridView1.PageIndex + 1) + " / " + GridView1.PageCount + "　页";*/
    }
    protected void rdoBtnCheckSci_CheckedChanged(object sender, EventArgs e)
    {
        searchtype = "科研机构";
        GridViewBind(searchtype);
        //显示当前页数
        lblPageSum.Text = "当前页为　" + (GridView1.PageIndex + 1) + " / " + GridView1.PageCount + "　页";
       /* GridView1.PageIndex = 0;
        GridView1.DataSource = operation.SelectInfo(infoType, false);
        GridView1.DataBind();
        CheckType = 0;
        //显示当前页数
        lblPageSum.Text = "当前页为　" + (GridView1.PageIndex + 1) + " / " + GridView1.PageCount + "　页";*/
    }
    protected void rdoBtnCheckSpe_CheckedChanged(object sender, EventArgs e)
    {
        searchtype = "专业人士";
        GridViewBind(searchtype);
        //显示当前页数
        lblPageSum.Text = "当前页为　" + (GridView1.PageIndex + 1) + " / " + GridView1.PageCount + "　页";
       /* GridView1.PageIndex = 0;
        GridViewBind(infoType);
        CheckType = -1;
        //显示当前页数
        lblPageSum.Text = "当前页为　" + (GridView1.PageIndex + 1) + " / " + GridView1.PageCount + "　页";*/
    }
    protected void rcm_Click(object sender, EventArgs e)
    {
        GridViewBind(searchtype);
       /* GridView1.Visible = true;
        lblPageSum.Visible = true;
        rdoBtnCheckSpe.Visible = true;
        rdoBtnCheckSci.Visible = true;
        rdoBtnCheckBus.Visible = true;
        btRcm.Visible = true;
        se.Visible = true;
        */
        
    }
    
    public void sendMail(string toEmailstring, string body)
    {
        string FROM = "dmdm2802@gmail.com";
        string SUBJECT = "推荐信息";
        string BODY = body;
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(FROM,"科研成果超市");
        msg.To.Add(toEmailstring);
        msg.Subject = SUBJECT;
        msg.Body = BODY;
        msg.BodyEncoding = System.Text.Encoding.UTF8;
        msg.IsBodyHtml = true;
        msg.ReplyTo = new MailAddress(FROM);
        //smtp.126.com不可用，所有使用smtp.gmail.com
        SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
        smtp.Credentials = new NetworkCredential("dmdm2802", "<pwd>2010");
        smtp.EnableSsl = true;
        try
        {
            smtp.Send(msg);
        }
        catch (Exception e) { }
    }
    protected void btRcm_Click(object sender, EventArgs e)
    {
        try
        {
            DataSet ds = operation.SelectInfo(Convert.ToInt32(infoid));
            string txtType = ds.Tables[0].Rows[0][1].ToString();
            string txtTitle = ds.Tables[0].Rows[0][2].ToString();
            string txtInfo = ds.Tables[0].Rows[0][3].ToString();
            string txtLinkMan = ds.Tables[0].Rows[0][4].ToString();
            string txtTel = ds.Tables[0].Rows[0][5].ToString();
            string date = ds.Tables[0].Rows[0][7].ToString();
            string msg = "信息类型：" + txtType + "<br/>" + "信息标题：" +
            txtTitle + "<br/>" + "详细内容：" +
            txtInfo + "<br/>" + "信息发布者：" +
            txtLinkMan + "<br/>" + "发布者电话：" +
            txtTel;
                    if (txtType.IndexOf("需求") != -1)
                    {
                        msg = "信息类型：" + txtType + "<br/>" + "公司名称：" + ds.Tables[0].Rows[0][10].ToString() + "<br/>项目名称：" +
                            ds.Tables[0].Rows[0][2].ToString() + "<br/>项目负责人：" + ds.Tables[0].Rows[0][4].ToString() + "  部门：" +
                             ds.Tables[0].Rows[0][11].ToString() + "  职务/职称：" + ds.Tables[0].Rows[0][12].ToString() + "<br/>通讯地址：" +
                             ds.Tables[0].Rows[0][13].ToString() + "  邮政编码：" + ds.Tables[0].Rows[0][14].ToString() +
                             "<br/> 联系电话：" + ds.Tables[0].Rows[0][5].ToString() + "  移动电话：" + ds.Tables[0].Rows[0][15].ToString() +
                             "<br/>传真号码：" + ds.Tables[0].Rows[0][16].ToString() + "   电子邮箱：" + ds.Tables[0].Rows[0][17].ToString() +
                             "<br/>希望合作的方式和要求：<br/>" + ds.Tables[0].Rows[0][3].ToString() + "<br/>企业基本信息：<br/>" + ds.Tables[0].Rows[0][19].ToString() +
                             "<br/>企业在生产过程中希望通过产学研合作解决的主要技术问题：<br/>" + ds.Tables[0].Rows[0][18].ToString() +
                             "<br/>企业希望开发的新产品、新技术：<br/>" + ds.Tables[0].Rows[0][20].ToString() +
                             "<br/>如果高校、科研院所有新的科研成果是否有意向开展产学研合作?有何要求？<br/>" + ds.Tables[0].Rows[0][21].ToString();
                    }
            int count = GridView1.Rows.Count;
            bool slt = false;
            for (int i = 0; i < count; i++)
            {
                if (GridView1.Rows[i].Cells[6].Controls.Count > 0)
                {
                    //CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[6].Controls[0];
                    CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[6].FindControl("cb");
                    if (cb.Checked)
                    {
                        slt = true;
                        if (operation.SelectRecommendInfo(GridView1.Rows[i].Cells[0].Text, infoid).Tables[0].Rows.Count <= 0)
                        {
                            operation.InsertRecommendInfo(GridView1.Rows[i].Cells[0].Text, infoid);
                            sendMail(GridView1.Rows[i].Cells[3].Text, "你好，以下是科研成果超市为你推荐的信息<br/><br/>" + msg);
                        }
                    }
                }
            }
            if (slt)
                Response.Write("<script language='javascript' type='text/javascript'>alert('推荐成功！');</script>");
            else
                Response.Write("<script language='javascript' type='text/javascript'>alert('你未选择任何推荐用户！');</script>");
        }
        catch (Exception ex) { Response.Write(ex.ToString()); }
    }

    
    protected void BtSearch_Click(object sender, EventArgs e)
    {
        if (umsg.Text != "")
        {
            int sl = Convert.ToInt32(dlSearchType.Text);
            if (sl == 1)
            {
                try
                {
                    Convert.ToInt32(umsg.Text);//用于排除非数字字串
                    GridView1.PageIndex = 0;
                    GridView1.DataSource = operation.SelectUserInfo(umsg.Text.Trim());
                    GridView1.DataBind();
                }
                catch (Exception ex)
                {
                    GridView1.PageIndex = 0;
                    //GridView1.DataSource = operation.SelectUserInfo(umsg.Text.Trim());
                    GridView1.DataBind();
                }
            }
            else if (sl == 2)
            {
                GridView1.PageIndex = 0;
                GridView1.DataSource = operation.ValidateUser(umsg.Text.Trim());
                GridView1.DataBind();
            }
            else if (sl == 3)
            {
                GridView1.PageIndex = 0;
                GridView1.DataSource = operation.ValidateEmail(umsg.Text.Trim());
                GridView1.DataBind();
            }
        }
        dlSearchType.Text = "0";
        //显示当前页数
        lblPageSum.Text = "当前页为　" + (GridView1.PageIndex + 1) + " / " + GridView1.PageCount + "　页";
    }
    protected void back_Click(object sender, EventArgs e)
    {
        string Src = "DetailInfo.aspx?id=" + infoid;
        Response.Write("<script language='javascript' type='text/javascript'>location.href='" + Src + "'</script>");
    }
}
